package com.y.surfacedemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by youjingjing on 2017/12/28.
 */

public class DrawBoard extends SurfaceView implements SurfaceHolder.Callback{
    SurfaceHolder surfaceHolder;
    boolean isdraw;
    Canvas canvas;
    Paint paint;
    Path path;
    float lastX,lastY;
    public DrawBoard(Context context) {
        super(context);
        init();
    }

    public DrawBoard(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DrawBoard(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

    }

    private void init() {
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setKeepScreenOn(true);

        path = new Path();
        paint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DITHER_FLAG);
        paint.setStrokeWidth(10f);
        paint.setColor(Color.parseColor("#FF4081"));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int wSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int wSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int hSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int hSpecSize = MeasureSpec.getSize(heightMeasureSpec);

        if (wSpecMode == MeasureSpec.AT_MOST && hSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(300, 300);
        } else if (wSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(300, hSpecSize);
        } else if (hSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(wSpecSize, 300);
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        isdraw = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isdraw){
                    drawing();
                }
            }
        }).start();
    }


    public void drawing(){
        try {
            canvas = surfaceHolder.lockCanvas();
            canvas.drawColor(Color.WHITE);
            canvas.drawPath(path,paint);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (canvas!=null){
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
      float x = event.getX();
      float y = event.getY();
      switch (event.getAction()){
          case MotionEvent.ACTION_DOWN:
              lastX = x;
              lastY = y;
              path.moveTo(x,y);
              break;
          case MotionEvent.ACTION_MOVE:
              float dx = Math.abs(x - lastX);
              float dy = Math.abs(y - lastY);
              if (dx >= 3 || dy >= 3) {
                  path.quadTo(lastX, lastY, (lastX + x) / 2, (lastY + y) / 2);
              }
              lastX = x;
              lastY = y;
              break;
          case MotionEvent.ACTION_UP:
              break;
      }
       return true;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        isdraw = false;
    }
}
